// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
#pragma once

namespace Weathr
{
    public enum class TemperatureUnit
    {
        Celsius,
        Fahrenheit
    };

    // Holds app-specific user-defined settings.
    [Windows::UI::Xaml::Data::Bindable]
    [Windows::Foundation::Metadata::WebHostHidden]
    public ref class UserSettings sealed : Windows::UI::Xaml::Data::INotifyPropertyChanged
    {
    public:
        virtual event Windows::UI::Xaml::Data::PropertyChangedEventHandler^ PropertyChanged;

        property bool IsCelsiusSet
        {
            bool get()
            {
                return TemperatureUnit == Weathr::TemperatureUnit::Celsius;
            }
            void set(bool value);
        }

        property bool IsFahrenheitSet
        {
            bool get()
            {
                return TemperatureUnit == Weathr::TemperatureUnit::Fahrenheit;
            }
            void set(bool value);
        }

        property bool ShowWeatherEffects
        {
            bool get()
            {
                return m_showWeatherEffects;
            }
            void set(bool value)
            {
                if (m_showWeatherEffects != value)
                {
                    m_showWeatherEffects = value;
                    SaveProperty("ShowWeatherEffects", value);
                    OnPropertyChanged("ShowWeatherEffects");
                }
            }
        }

        property bool ShowStarfield
        {
            bool get()
            {
                return m_showStarfield;
            }
            void set(bool value)
            {
                if (m_showStarfield != value)
                {
                    m_showStarfield = value;
                    SaveProperty("ShowStarfield", value);
                    OnPropertyChanged("ShowStarfield");
                }
            }
        }

        property bool ShowDebugOutput
        {
            bool get()
            {
                return m_showDebugOutput;
            }
            void set(bool value)
            {
                if (m_showDebugOutput != value)
                {
                    m_showDebugOutput = value;
                    SaveProperty("ShowDebugOutput", value);
                    OnPropertyChanged("ShowDebugOutput");
                }
            }
        }

    internal:
        static property UserSettings^ Current
        {
            UserSettings^ get();
        }

    private:
        property TemperatureUnit TemperatureUnit
        {
            Weathr::TemperatureUnit get()
            {
                return m_temperatureUnit;
            }
            void set(Weathr::TemperatureUnit value)
            {
                if (m_temperatureUnit != value)
                {
                    m_temperatureUnit = value;
                    // Convert enum value to string.
                    SaveProperty("TemperatureUnit", static_cast<Weathr::TemperatureUnit>(value) == Weathr::TemperatureUnit::Celsius ? "Celsius" : "Fahrenheit");
                    OnPropertyChanged("TemperatureUnit");
                    OnPropertyChanged("IsCelsiusSet");
                    OnPropertyChanged("IsFahrenheitSet");
                }
            }
        }

        static UserSettings^ m_current;
        Weathr::TemperatureUnit m_temperatureUnit;
        bool m_showWeatherEffects;
        bool m_showStarfield;
        bool m_showDebugOutput;

        UserSettings();

        void SaveProperty(Platform::String^ name, Platform::Object^ value);

        inline void OnPropertyChanged(Platform::String^ propertyName)
        {
            PropertyChanged(this, ref new Windows::UI::Xaml::Data::PropertyChangedEventArgs(propertyName));
        }
    };
}